export default {
    /**
     * 获取变量的类型
     */
    typ: (function () {
        var type = {};
        var typeArr = ['String', 'Object', 'Number', 'Array', 'Undefined', 'Function', 'Null', 'Symbol'];
        for (var i = 0; i < typeArr.length; i++) {
            (function (name) {
                type['Is' + name] = function (obj) {
                    return Object.prototype.toString.call(obj) == '[object ' + name + ']';
                }
            })(typeArr[i]);
        }
        return type;
    })(),

    /**
     * 移除json字符串中的null或undefined，如果有嵌套对象遍历移除
     * @param jsonData
     */
    removeJsonNull: function (jsonData) {
        for(let i in jsonData){
            if (jsonData[i] == undefined || jsonData[i] == null) {
                delete  jsonData[i]
            } else if (this.typ.IsObject(jsonData[i])) {
                this.removeJsonNull(jsonData[i])
            }
        }
    },

    /**
     *
     * 是否重复点击
     * @param event
     * @returns {boolean}
     */
    isNotAgainClick:function (event) {
        if(!event.target.clickTimestamp){
            event.target.clickTimestamp = event.timestamp
            return true
        }else{
            if(event.timestamp - event.target.clickTimestamp > 300){
                event.target.clickTimestamp = event.timestamp
                return true
            }else{
                event.target.clickTimestamp = event.timestamp
                return false
            }
        }
    },

    /**
     * 事件状态的锁定延迟释放
     * @param event
     */
    lockEventDeferFree:function (event) {
        if(event.target.clickIsLock) {
            return true
        }
        event.target.clickIsLock = true
        setTimeout(()=>{
            event.target.clickIsLock = false
        },2000)
        return false

    },

    /**
     * 字符串是否为null，undefined 或空字符串
     * @param string
     * @returns {boolean}
     */
    isNullOrEmpty:function (string) {
        if (string == null || string == undefined) {
            return true
        }

        if (string.trim() == '') {
            return true
        }

        return false
    },
    /**
     * 判断字符串，数组，对象是否为空
     * 字符判断是否为空字符串
     * 数组判断是否存在元素
     * 对象判断是否存在键值对
     * @param obj
     * @returns {boolean}
     */
    isEmpty:function (obj) {
        if (obj == null || obj == undefined) {
            return false
        }

        if (typeof obj == 'string' && obj.trim() == '') {
            return true
        }

        if (typeof obj == 'object') {
            if (obj instanceof Array && obj.length == 0) {
                 return true
            }else{
                let num = 0
                for (let i in obj){
                    num++
                }
                if (num == 0) {
                    return true
                }
            }
        }

        return false
    },
    /**
     * 判断字符串，数组，对象是否不为空
     * @function isEmpy 取反
     * @param obj
     * @returns {boolean}
     */
    isNotEmpty:function (obj) {
        return !this.isEmpty(obj)
    },


    //生成从minNum到maxNum的随机数
    /**
     * 获取随机数
     * @param minNum
     * @param maxNum
     * @returns {number}
     */
    randomNum:function (minNum,maxNum){
        switch(arguments.length){
            case 1:
                return parseInt(Math.random()*minNum+1,10)
                break
            case 2:
                return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10)
                break
            default:
                return 0
                break
        }
    }

}
